//
//  AppDelegate.m
//  sc-cyan-ios
//
//  Created by lvjian01 on 2017/12/14.
//  Copyright © 2017年 greenbuilding. All rights reserved.
//

#import "AppDelegate.h"

#import "LLTabBar.h"
#import <Reachability.h>

 #import <BaiduMapAPI_Map/BMKMapView.h>//只引入所需的单个头文件

@interface AppDelegate ()<UITabBarControllerDelegate>
{
    BMKMapManager* _mapManager;
}
@end

@implementation AppDelegate


- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    // Override point for customization after application launch.
    
    [self setUpMapView];
    
    self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
    self.window.backgroundColor = [UIColor whiteColor];
    [self.window makeKeyAndVisible];
    
    self.tabBarController = [self shareHomeTabController];
    
    self.window.rootViewController = _tabBarController;
//    [[UIApplication sharedApplication] setStatusBarHidden:NO withAnimation:UIStatusBarAnimationFade];
    
    
    
    return YES;
}

// 设置地图
- (void)setUpMapView{
    
    _mapManager = [[BMKMapManager alloc]init];
    // 如果要关注网络及授权验证事件，请设定     generalDelegate参数
    BOOL ret = [_mapManager start:@"eIX7npQsGplNBdRiXoAStraV1lagP3s3"  generalDelegate:nil];
    if (!ret) {
        NSLog(@"manager start failed!");
    }
}

- (UITabBarController *)shareHomeTabController{
    static UITabBarController *tabBarController;
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        tabBarController = [[UITabBarController alloc] init];
        tabBarController.delegate = self;
        [[UITabBar appearance] setBackgroundImage:[[UIImage alloc] init]];
        [[UITabBar appearance] setShadowImage:[[UIImage alloc] init]];
        
        LLTabBar *tabBar = [[LLTabBar alloc] initWithFrame:tabBarController.tabBar.bounds];
        tabBar.tag = 8080;
        tabBar.tabBarItemAttributes = @[
                                        @{kLLTabBarItemAttributeTitle:@"首页",
                                          kLLTabBarItemAttributeNormalImageName:@"sy_sy",
                                          kLLTabBarItemAttributeSelectedImageName:@"sy_sy01",
                                          kLLTabBarItemAttributeType:@(LLTabBarItemNormal),kLLTabBarItemAttributeNeedDoSomeThing:@(NO)},
                                        @{kLLTabBarItemAttributeTitle:@"分类",
                                          kLLTabBarItemAttributeNormalImageName:@"sy_fenlei",
                                          kLLTabBarItemAttributeSelectedImageName:@"sy_fenlei01",
                                          kLLTabBarItemAttributeType:@(LLTabBarItemNormal),kLLTabBarItemAttributeNeedDoSomeThing:@(NO)},
                                        @{kLLTabBarItemAttributeTitle:@"收藏",
                                          kLLTabBarItemAttributeNormalImageName:@"sy_shoucang",
                                          kLLTabBarItemAttributeSelectedImageName:@"sy_shoucang01",
                                          kLLTabBarItemAttributeType:@(LLTabBarItemNormal),kLLTabBarItemAttributeNeedDoSomeThing:@(NO)},
                                        @{kLLTabBarItemAttributeTitle:@"我的",
                                          kLLTabBarItemAttributeNormalImageName:@"sy_wode",
                                          kLLTabBarItemAttributeSelectedImageName:@"sy_wode01",
                                          kLLTabBarItemAttributeType:@(LLTabBarItemNormal),kLLTabBarItemAttributeNeedDoSomeThing:@(NO)}
                                        ];
        
        tabBarController.viewControllers = @[
                                             [[UIStoryboard storyboardWithName:@"HomePage" bundle:[NSBundle mainBundle]] instantiateInitialViewController],
                                             [[UIStoryboard storyboardWithName:@"LeaseClassification" bundle:[NSBundle mainBundle]] instantiateInitialViewController],
                                             [[UIStoryboard storyboardWithName:@"Collection" bundle:[NSBundle mainBundle]] instantiateInitialViewController],
                                             [[UIStoryboard storyboardWithName:@"Mine" bundle:[NSBundle mainBundle]] instantiateInitialViewController]
                                             ];
        [tabBarController.tabBar addSubview:tabBar];
    });
    return tabBarController;
}


- (void)applicationWillResignActive:(UIApplication *)application {
    // Sent when the application is about to move from active to inactive state. This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) or when the user quits the application and it begins the transition to the background state.
    // Use this method to pause ongoing tasks, disable timers, and invalidate graphics rendering callbacks. Games should use this method to pause the game.
}


- (void)applicationDidEnterBackground:(UIApplication *)application {
    // Use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated later.
    // If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits.
}


- (void)applicationWillEnterForeground:(UIApplication *)application {
    // Called as part of the transition from the background to the active state; here you can undo many of the changes made on entering the background.
}


- (void)applicationDidBecomeActive:(UIApplication *)application {
    // Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface.
}


- (void)applicationWillTerminate:(UIApplication *)application {
    // Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:.
}

- (void)checkNetWorkingWithReachibility{
    
    
    //    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(reachabilityChanged:) name:kReachabilityChangedNotification object:nil];
    //    self.hostReach = [Reachability reachabilityWithHostName:dlpath(@"")] ;
    
    //    self.hostReach = [Reachability reachabilityWithHostName:@"www.baidu.com"] ;
    
    //开始监听，会启动一个run loop
    
    //    [self.hostReach startNotifier];
    
    //    Reachability *reach = [Reachability reachabilityWithHostname:@"www.baidu.com"];
    Reachability *reach = [Reachability reachabilityForInternetConnection];
    
    //    reach.reachableOnWWAN = YES;
    self.isReachable = YES;
    
    if (!reach.isReachable) {
        UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"网络连接异常" message:nil delegate:nil cancelButtonTitle:@"确定" otherButtonTitles:nil];
        [alert show];
        self.isReachable = NO;
    }
    
    
    reach.reachableBlock = ^(Reachability *reachability) {
        dispatch_async(dispatch_get_main_queue(), ^{
            NSLog(@"网络连接成功");
            self.isReachable = YES;
            
        });
    };
    
    reach.unreachableBlock = ^(Reachability *reachability) {
        
        dispatch_async(dispatch_get_main_queue(), ^{
            NSLog(@"网络连接异常");
            UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"网络连接异常" message:nil delegate:nil cancelButtonTitle:@"确定" otherButtonTitles:nil];
            [alert show];
            
            self.isReachable = NO;
        });
        
    };
    
    [reach startNotifier];
    
    
}



@end
